package com.mobile.learning.lib;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xmlrpc.android.XMLRPCClient;

import com.mobile.learning.LoginActivity;

import android.R.integer;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class ServiceHelper {

	private static final int NULL = 0;
	private URI uri = URI
			.create("http://50.50.50.34/newtonsix/index.php/service");
	private XMLRPCClient client = new XMLRPCClient(uri);
	String ImgUrl1 = "http://50.50.50.34/newtonsix/images/real/";
	String ImgUrl2 = "http://50.50.50.34/newtonsix/images/small/";

	public boolean login(Context contex, String id, String pwd) {
		DBClass db = new DBClass(contex);
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = new Date();

		try {
			String text = (String) client.call("login", id, pwd);
			JSONArray jsonArray = new JSONArray("[" + text + "]");
			JSONArray innerJsonArray = jsonArray.getJSONArray(0);
			JSONObject json = innerJsonArray.getJSONObject(0);
			String stsNI = json.getString("nomor_induk");
			String stsPass = json.getString("password");
			if (stsNI.equals(id) && stsPass.equals(pwd)) {

				db.InsertSiswa(new Login(NULL, id, json.getString("nama"), pwd,
						dateFormat.format(date)));

				Log.d("Status", "Login");
				return true;
			} else {
				Log.d("Status", "Gagal");

				return false;
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}

	}

	public boolean UpdateTask(Context context, String jenis) {
		DBClass db = new DBClass(context);

		try {
			String text = (String) client.call("getjenis", jenis);
			JSONArray jsonArray = new JSONArray("[" + text + "]");
			JSONArray innerJsonArray = jsonArray.getJSONArray(0);
			if (text != null) {
				for (int i = 0; i < innerJsonArray.length(); i++) {
					JSONObject json = innerJsonArray.getJSONObject(i);
					int id_jenis = Integer.parseInt(json.getString("id_jenis"));
					db.InsertJenis(new Jenis(id_jenis, json
							.getString("jenis_soal"), json
							.getString("kode_jenis"), json
							.getString("nama_jenis"), json
							.getString("versi_soal")));

					String text2 = (String) client.call("getsoal", id_jenis);
					JSONArray jsonArray2 = new JSONArray("[" + text2 + "]");
					JSONArray innerJsonArray2 = jsonArray2.getJSONArray(0);
					// Log.d("inner2", innerJsonArray2.toString());
					if (text2 != null) {
						for (int j = 0; j < innerJsonArray2.length(); j++) {
							JSONObject json2 = innerJsonArray2.getJSONObject(j);
							int id_soal = Integer.parseInt(json2
									.getString("id_soal"));
							int id_jenis2 = Integer.parseInt(json2
									.getString("id_jenis"));
							db.InsertSoal(new Soal(id_soal, json2
									.getString("text_soal"), id_jenis2, json2
									.getString("filegambar")));

							String text3 = (String) client.call("getjawaban",
									id_soal);
							JSONArray jsonArray3 = new JSONArray("[" + text3
									+ "]");
							JSONArray innerJsonArray3 = jsonArray3
									.getJSONArray(0);
							// Log.d("inner3", innerJsonArray3.toString());
							if (text3 != null) {
								for (int k = 0; k < innerJsonArray3.length(); k++) {
									JSONObject json3 = innerJsonArray3
											.getJSONObject(k);
									int id_jawaban = Integer.parseInt(json3
											.getString("id_jawaban"));
									int id_soal2 = Integer.parseInt(json3
											.getString("id_soal"));
									// Log.d("inner3",
									// json3.getString("id_soal"));
									db.InsertJawaban(new Jawaban(id_jawaban,
											json3.getString("text_jawaban"),
											json3.getString("values"),
											id_soal2, json3
													.getString("filegambar")));
								}
							}

						}
					}
				}

				String text4 = (String) client.call("getimageall");
				JSONArray json = new JSONArray(text4);
				for (int i = 0; i < json.length(); i++) {
					JSONObject js = json.getJSONObject(i);
					MCrypt mc = new MCrypt();
					byte[] en;
					if (!js.getString("gambarsoal").equals("null")) {
						try {
							en = mc.encrypt(js.getString("gambarsoal"));
							InternetHelper inet = new InternetHelper();
							inet.downloadImage(
									ImgUrl1 + js.getString("gambarsoal"),
									mc.bytesToHex(en));
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
					}
					if (!js.getString("gambarjawaban").equals("null")) {
						try {
							en = mc.encrypt(js.getString("gambarjawaban"));
							InternetHelper inet = new InternetHelper();
							inet.downloadImage(
									ImgUrl2 + js.getString("gambarjawaban"),
									mc.bytesToHex(en));
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
					}
				}

				Log.d("Status", "Data Inserted !");
				return true;

			} else {
				Toast.makeText(context, "Gagal update Soal !",
						Toast.LENGTH_SHORT).show();
				return false;
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.d("Status", "Failed");
			e.printStackTrace();
			return false;
		}
	}
}
